package cn.edu.nwsuaf.examcenter.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import cn.edu.nwsuaf.examcenter.model.PageModel;
import cn.edu.nwsuaf.examcenter.pojo.Student;
import cn.edu.nwsuaf.examcenter.service.StudentService;

@Service(value="studentService")
public class StudentServiceImpl extends BaseServiceImpl<Student> implements StudentService {

	@Override
	public String addStudent(Student student) {
		// TODO Auto-generated method stub
		student.setStudentPassword(student.getStudentIdcard().substring(12,18));
		super.saveData(student);
		return "success";
	}

	@Override
	public String deleteStudent(String studentNo) {
		// TODO Auto-generated method stub
		super.deleteData(super.loadById(Student.class, studentNo));
		return "success";
	}

	@Override
	public String deleteStudentList(String array) {
		// TODO Auto-generated method stub
		String[] studentNos = array.split(",");
		List<Student> list = new ArrayList<>();
		for(String studentNo:studentNos){
			list.add(super.loadById(Student.class, studentNo));
		}
		super.deleteDataList(list);
		return "success";
	}

	@Override
	public void loadStudent(ModelMap modelMap) {
		// TODO Auto-generated method stub
		PageModel<Student> pageModel = new PageModel<>();
		List<Student> list = super.findAll(Student.class);
		pageModel.setTotalrows(list.size());
		pageModel.setList(list);
		modelMap.addAttribute("pageModel",pageModel);
	}

	@Override
	public void exportStudent(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<String> getStudentNoBySclassNo(List<String> classNoList) {
		// TODO Auto-generated method stub
		List<String> studentNoList = new ArrayList<>();
		for(String sclassNo:classNoList){
			String hql = "FROM Student as student where student.sclass.classNo=?";
			String[] param = {sclassNo};
			List<Student> studentList = super.findAll(hql, param);
			for(Student s:studentList){
				studentNoList.add(s.getStudentNo());
			}
		}
		return studentNoList;
	}

	@Override
	public String getMyClassNo(String studentNo) {
		// TODO Auto-generated method stub
		return super.getById(Student.class, studentNo).getSclass().getClassNo();
	}

}
